package dreamCookGame;

import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.Random;

import javax.swing.JPanel;
import javax.swing.Timer;

import dreamCookGame.chickenMiniGame.ChickenMiniGame;
import dreamCookGame.conveyorBeltMiniGame.ConveyorBeltMiniGame;
import dreamCookGame.fishMiniGame.FishMiniGame;
import dreamCookGame.puzzleMiniGame.PuzzleMiniGame;

public class Controller {
	EventHandler _eventHandler;
	MainScreen _mainScreen;
	BottomMenu _bottomMenu1;
	BottomMenuButtons _bottomMenuButtons;
	GameState _gameState;
	Scorer _scorer;
	Location _optionsMenu;
	//Inventory _inventory;
	FishMiniGame _fishMiniGame;
	PuzzleMiniGame _puzzleMiniGame;
	ChickenMiniGame _chickenMiniGame;
	ConveyorBeltMiniGame _conveyorBeltMiniGame;
	Menu _bottomMenu;
	Item _testItem;
	Timer _timer;
	Timer _miniGameTimer;
	Item _selectedItem;
	Character _selectedCharacter;
	boolean _characterClicked;
	boolean _videoOn;
	
	//Mini-game flags 
	boolean _fishMiniGameOn;
	boolean _puzzleGameOn;
	boolean _chickenMiniGameOn;
	boolean _conveyorBeltMiniGameOn;
	
	//More flags
	boolean _itemMenuOpen;
	boolean _inventoryOn;
	boolean _soundOn;
	
	public Controller() {
		_eventHandler = new EventHandler(this);
		_mainScreen = new MainScreen(800, 600);
		_gameState = new GameState();
		//_audioPlayer = new AudioPlayer();

		_selectedItem = null;
		_selectedCharacter = null;
		
		//Set flags to initial values
		_fishMiniGameOn = false;
		_puzzleGameOn = false;
		_chickenMiniGameOn = false;
		_conveyorBeltMiniGameOn = false;
		_itemMenuOpen = false;
		_inventoryOn = false;
		_characterClicked = false;
		
		createBottomMenu();
		createOptionsMenu();	
		
		//OTHER
		_soundOn = true;
		//String bearWalkingImgNames[] = {"bearImage", "bearImage2", "bearImage", "bearImage2", "bearImage"};
		//_mainCharacter = new Character("Bear", "bearImage", bearWalkingImgNames, 50, 300);
		String[] testItemMenuOptions = {"addToInventory"};

		createTimer();
		
		initializeBeginningLocation();
		loadCurrentGameState();
		
		//createFishMiniGame();
		//createPuzzleMiniGame();
		//createChickenMiniGame();
		//createConveyorBeltMiniGame();
	}
	
	private void createTimer() {
		int _MILLESECONDS_BETWEEN_FRAMES = 100;
		_timer = new Timer(_MILLESECONDS_BETWEEN_FRAMES, new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				_timer.stop();
				step();
				if(_videoOn)
					if(	_mainScreen._currentVideo._videoFinished)
						stopCutScene();
				_timer.restart();
			}

			private void step() {
				_gameState._currentLocation.updateAllCharacters();
				_mainScreen.redraw();
			}
		});
		_timer.start();
	}
	
	private void createBottomMenu() {
		//BUTTONS
		String[] satchelBtnStateNames = {"bottomMenuSatchelBtn"};
		String[] menuBtnStateNames = {"bottomMenuMenuBtn"};
		String[] volumeBtnStateNames = {"bottomMenuVolumeBtn", "bottomMenuVolumeCancelBtn"};
			//Button attributes
		
			int bottomMenuWidth = 279;
			int bottomMenuHeight = 110;
			int bottomMenuX = _mainScreen._width-bottomMenuWidth;
			int bottomMenuY = _mainScreen._height-bottomMenuHeight;
		
			int satchelWidth = 120;
			int satchelHeight = 84;
			int satchelX = bottomMenuX+12;
			int satchelY = bottomMenuY+3;
			
			int menuBtnWidth = 74;
			int menuBtnHeight = 26;
			int menuBtnX = bottomMenuX+145;
			int menuBtnY = bottomMenuY+36;	
			
			int volumeBtnWidth = 29;
			int volumeBtnHeight = 70;
			int volumeBtnX = bottomMenuX+230;
			int volumeBtnY = bottomMenuY+59;
		
		Button satchelMenuBtn = new Button(satchelX, satchelY, new Rectangle(satchelX+8, satchelY+11, satchelWidth-9, satchelHeight), satchelBtnStateNames);
		Button menuMenuBtn = new Button(menuBtnX, menuBtnY, new Rectangle(menuBtnX+11, menuBtnY+11, menuBtnWidth, menuBtnHeight), menuBtnStateNames);
		Button volumeMenuBtn = new Button(volumeBtnX, volumeBtnY, new Rectangle(volumeBtnX+9, volumeBtnY+9, volumeBtnWidth, volumeBtnHeight), volumeBtnStateNames);
		
		Button[] bottomMenuBtns = {satchelMenuBtn, menuMenuBtn, volumeMenuBtn};
		
		//MENU
		_bottomMenu = new Menu(bottomMenuBtns, "bottomMenu", new Rectangle(bottomMenuX, bottomMenuY, bottomMenuWidth, bottomMenuHeight));
	}
	
	public void createOptionsMenu() {
		String[] saveGameBtnImgs = {"optionsMenuSaveGameBtn"};
		String[] loadGameBtnImgs = {"optionsMenuLoadGameBtn"};
		String[] exitGameBtnImgs = {"optionsMenuExitGameBtn"};
		String[] backBtnImgs = {"backBtn"};
		
		Item saveGameItem = new Item("saveGame", saveGameBtnImgs,  220, 110, null, null, null, false, null);
		Item loadGameItem = new Item("loadGame", loadGameBtnImgs,  190, 272, null, null, null, false, null);
		Item exitGameItem = new Item("exitGame", exitGameBtnImgs,  330, 415, null, null, null, false, null);
		Item backItem = new Item("exitGame", backBtnImgs,  10, 450, null, null, null, false, null);
		
		Item[] optionsMenuItems = {saveGameItem, loadGameItem, exitGameItem, backItem};
		
		_optionsMenu = new Location("optionsMenu", "OptionsMenuBackground", null, 0, 0, null, optionsMenuItems, null, 0, 0);
	}

	public void initializeBeginningLocation() {
		_gameState.loadCurrentLocation(_gameState._plotPointReference._kitchen);
	}
	
	private void incrementPlotPoint() {
		_gameState._currentPlotPoint += 1;
		loadCurrentGameState();
	}
	
	private void loadCurrentGameState() {
		Location moveLocation = _gameState._plotPointReference._plotPoints[_gameState._currentPlotPoint]._moveLocation;
		Video cutScene = _gameState._plotPointReference._plotPoints[_gameState._currentPlotPoint]._cutScene;
		if(cutScene != null){
			//playCutScene();
		}
		if( moveLocation != null ) {
			changeLocation(moveLocation);
		}
		else{
			_gameState.updateCurrentLocationFromPlotPoint();
			_mainScreen._currentScene = _gameState._currentLocation;
			_mainScreen.loadScene(_gameState._currentLocation, _bottomMenu);
			_mainScreen.redraw();
		}
	}

	private void changeLocation(Location location) {
		
		_gameState.loadCurrentLocation(location);
		_gameState.updateCurrentLocationFromPlotPoint();
		_mainScreen._currentScene = _gameState._currentLocation;
		_mainScreen.loadScene(_gameState._currentLocation, _bottomMenu);
		_mainScreen.redraw();
		
	}
	
	private void playCutScene(){
		_videoOn = true;
		Video video = new Video("test", 10);
		_mainScreen.displayVideo(_gameState._plotPointReference._plotPoints[_gameState._currentPlotPoint]._cutScene);

	}
	
	private void stopCutScene(){
		_videoOn = false;
		_mainScreen.stopVideo();
		_mainScreen.redraw();	
	}
	
	private void createFishMiniGame(){
		_fishMiniGame = new FishMiniGame("fishBackground", "fishForeground");
		_mainScreen.loadScene(_fishMiniGame, null);
		_fishMiniGameOn = true;
	
		int _MILLESECONDS_BETWEEN_FRAMES = 100;
		
		_miniGameTimer = new Timer(_MILLESECONDS_BETWEEN_FRAMES, new ActionListener(){
			public void actionPerformed(ActionEvent event){
				_miniGameTimer.stop();
				generate();
				_miniGameTimer.restart();
			}
	
			private void generate() {
				_fishMiniGame.moveFish();
				_mainScreen.redraw();
			}
		});
		_miniGameTimer.start();
			
	}

	private void createPuzzleMiniGame(){
		_puzzleMiniGame = new PuzzleMiniGame("puzzleBackground");
		_mainScreen.loadScene(_puzzleMiniGame, null);
		_puzzleGameOn = true;
	}
	
	private void createChickenMiniGame() {
		_chickenMiniGame = new ChickenMiniGame();
		_mainScreen.loadScene(_chickenMiniGame, null);
		_chickenMiniGameOn = true;
		
		int _MILLESECONDS_BETWEEN_FRAMES = 50;
		_miniGameTimer = new Timer(_MILLESECONDS_BETWEEN_FRAMES, new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				_miniGameTimer.stop();
				step();
				if(!_chickenMiniGame._gameDone){
					_miniGameTimer.restart();
				}else{
					_miniGameTimer.stop();
					_chickenMiniGameOn = false;
					_chickenMiniGame = null;
					incrementPlotPoint();
				}
				
			}

			private void step() {
				_chickenMiniGame.update();
				_mainScreen.redraw();
			}
		});
		_miniGameTimer.start();
	}
	
	private void createConveyorBeltMiniGame() {
		_conveyorBeltMiniGame = new ConveyorBeltMiniGame(this._mainScreen._reference);
		_mainScreen.loadScene(_conveyorBeltMiniGame, null);
		_conveyorBeltMiniGameOn = true;
		
		int _MILLESECONDS_BETWEEN_FRAMES = 100;
		_miniGameTimer = new Timer(_MILLESECONDS_BETWEEN_FRAMES, new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				_miniGameTimer.stop();
				step();
				if(!_conveyorBeltMiniGame._gameDone){
					_miniGameTimer.restart();
				}
				else {
					_miniGameTimer.stop();
					_conveyorBeltMiniGameOn = false;
					_conveyorBeltMiniGame = null;
					incrementPlotPoint();
				}
			}

			private void step() {
				_conveyorBeltMiniGame.update();
				_mainScreen.redraw();
			}
		});
		_miniGameTimer.start();
	}

	public void processMouseEvent(String eventType, int x, int y) {
		if(eventType.equals("mouseClicked"))
			mouseClicked(x, y);
		else if(eventType.equals("mouseMoved"))
			mouseMoved(x, y);			
		
		_mainScreen.redraw();

	}
	
	
	public void processKeyEvent(char key) {
		//TODO: Take into account all mini games (Brittany)
		if(!_inventoryOn && !_fishMiniGameOn && !_chickenMiniGameOn && !_conveyorBeltMiniGameOn){
			Character mainCharacter = _gameState._currentLocation._characters[0];
			if(key == 'a'  || key == 'A'){
				if(mainCharacter._x > ((Location) _gameState._currentLocation).getXLeftLimit())
					mainCharacter.walkLeft();
			}	
			else if(key == 'd' || key == 'D'){
				if(mainCharacter._x< ((Location) _gameState._currentLocation).getXRightLimit())
					mainCharacter.walkRight();
			}
		}
		else if(_fishMiniGameOn) {
			int fishingPoleLocX = _mainScreen._currentScene._itemsBack[_mainScreen._currentScene._itemsBack.length-1]._x;
			int fishingPoleLocY = _mainScreen._currentScene._itemsBack[_mainScreen._currentScene._itemsBack.length-1]._y;
			
			if(key == 'a'  || key == 'A') {
				if(fishingPoleLocX > 20)
					_fishMiniGame.moveFishingLine('l');
			}	
			else if(key == 'd' || key == 'D') {
				if(fishingPoleLocX < 730)
					_fishMiniGame.moveFishingLine('r');
			}
			else if(key == 'w'  || key == 'W') {
				if(fishingPoleLocY > -500)
					_fishMiniGame.moveFishingLine('u');
			}	
			else if(key == 's' || key == 'S') {
				if(fishingPoleLocY < 0)
					_fishMiniGame.moveFishingLine('d');
			}
			
			if(_mainScreen._currentScene._itemsBack[0].isWithinItem(fishingPoleLocX+25, fishingPoleLocY+550)){
				_fishMiniGame._hooked = true;
				_fishMiniGame.checkIntersections();
			
			}
			
			if(_fishMiniGame._hooked && fishingPoleLocY == -500){
			    _fishMiniGameOn = false;
				//createFarm();
				//TODO: Add fish to inventory (Brittany)
			}
		}
		else if(_chickenMiniGameOn) {	
					if(key == 'a'  || key == 'A') {
						_chickenMiniGame.moveCharacter('l');
					}	
					else if(key == 'd' || key == 'D') {
						_chickenMiniGame.moveCharacter('r');
					}
					else if(key == 'w'  || key == 'W') {
						_chickenMiniGame.moveCharacter('u');
					}	
					else if(key == 's' || key == 'S') {
						_chickenMiniGame.moveCharacter('d');
					}
		}
		else if(_conveyorBeltMiniGameOn) {
			if(key == 'a'  || key == 'A') {
				_conveyorBeltMiniGame.moveBag('l');
			}	
			else if(key == 'd' || key == 'D') {
				_conveyorBeltMiniGame.moveBag('r');
			}
		}
		
		//ADD BACK IN BEFORE COMMITTING (ANALIA)
		//_mainScreen.redraw();
	}
	
	public void processReleasedKeyEvent(char key) {

		if(!_inventoryOn && !_fishMiniGameOn && !_chickenMiniGameOn && !_conveyorBeltMiniGameOn) {
			Character mainCharacter = _gameState._currentLocation._characters[0];
			if(key == 'a' || key == 'A'){
					mainCharacter.stopLeft();
			}	
			else if(key == 'd' || key == 'D'){
					mainCharacter.stopRight();
			}
		}
		else if(_chickenMiniGameOn){
			
			if(key == 'a' || key == 'A'){
				_chickenMiniGame.stopCharacter('l');
			}	
			else if(key == 'd' || key == 'D'){
				_chickenMiniGame.stopCharacter('r');
			}
			else if(key == 'w' || key == 'W'){
				_chickenMiniGame.stopCharacter('u');
			}	
			else if(key == 's' || key == 'S'){
				_chickenMiniGame.stopCharacter('d');
			}
		}
		else if(_conveyorBeltMiniGameOn) {
			if(key == 'a'  || key == 'A') {
				_conveyorBeltMiniGame.stopBag('l');
			}	
			else if(key == 'd' || key == 'D') {
				_conveyorBeltMiniGame.stopBag('r');
			}
		}
	}
	
	private void mouseClicked(int x, int y) {
		if(_mainScreen._bottomMenu != null)
			bottomMenuCheckMouseClickedAndUpdate(x, y);
		if(_itemMenuOpen)
			itemMenuCheckMouseClickedAndUpdate(x, y); 
		else {
			itemsCheckMouseClickedAndUpdate(x, y);
			charactersCheckMouseClickedAndUpdate(x, y);
		}
		
		if(_puzzleGameOn)
			puzzlePiecesCheckClickandUpdate(x, y);
		else if(_inventoryOn)
			inventoryCheckMouseClickedAndUpdate(x, y);
		else if(_gameState._currentLocation._name.equals("optionsMenu"))
			optionsMenuCheckMouseClickedAndUpdate(x, y);
		

		//REMOVE
		System.out.println("x: " + x + ", y: " + y);
	}

	private void mouseMoved(int x, int y) {
		if(_mainScreen._bottomMenu != null)
			bottomMenuCheckMouseMovedAndUpdate(x, y);
		if(_itemMenuOpen)
			itemMenuCheckMouseMovedAndUpdate(x, y);
		else
			itemsCheckMouseMovedAndUpdate(x, y);
	}
	
	private void bottomMenuCheckMouseMovedAndUpdate(int x, int y) {	
		Point point = new Point(x, y);
		
		if(_bottomMenu.isWithinMenu(point)) {
			for(int i = 0; i < _bottomMenu._buttons.length; i++) {
				if(_bottomMenu._buttons[i].checkIfOnButton(point))
					_bottomMenu._buttons[i].highlight();
				else
					_bottomMenu._buttons[i].unHighlight();
			}
		}
	}
	
	private void bottomMenuCheckMouseClickedAndUpdate(int x, int y) {
		Point point = new Point(x, y);
				
		if(_bottomMenu.isWithinMenu(point)) {
				if(_bottomMenu._buttons[_bottomMenuButtons.VOLUME.ordinal()].checkIfOnButton(point) && _soundOn) {
					_bottomMenu._buttons[_bottomMenuButtons.VOLUME.ordinal()].changeState(1);
					_soundOn = false;
				}
				else if(_bottomMenu._buttons[_bottomMenuButtons.VOLUME.ordinal()].checkIfOnButton(point) && !_soundOn) {
					_bottomMenu._buttons[_bottomMenuButtons.VOLUME.ordinal()].changeState(0);
					_soundOn = true;
				}
				else if(_bottomMenu._buttons[_bottomMenuButtons.SATCHEL.ordinal()].checkIfOnButton(point)) {
					if(!_inventoryOn) {
						_mainScreen.loadScene(_gameState._inventory, null);
						_gameState._inventory.layoutItems();//MUST be called *after* loading the inventory into mainScreen
						_inventoryOn = true;
					}
				}
				else if(_bottomMenu._buttons[_bottomMenuButtons.MENU.ordinal()].checkIfOnButton(point)) {
					if(_mainScreen._currentScene != _optionsMenu) {
						if(_inventoryOn){
							_gameState._inventory.restoreItemLocations();
							_inventoryOn = false;
						}
						_gameState.loadCurrentLocation(_optionsMenu);
						_mainScreen.loadScene(_optionsMenu, null);
					}
				}
		}
	}

	private void optionsMenuCheckMouseClickedAndUpdate(int x, int y) {
		Point point = new Point(x, y);
		if(_optionsMenu._itemsBack[0]._itemButton._boundingBox.contains(point))
			saveGame();
		else if(_optionsMenu._itemsBack[1]._itemButton._boundingBox.contains(point))
			loadGame();
		else if(_optionsMenu._itemsBack[2]._itemButton._boundingBox.contains(point))
			exitGame();
		else if(_optionsMenu._itemsBack[3]._itemButton._boundingBox.contains(point)) {
			_gameState.loadPreviousLocationAsCurrent();
			_mainScreen.loadScene(_gameState._currentLocation, _bottomMenu);
		}
		
	}

	private void inventoryCheckMouseClickedAndUpdate(int x, int y) {
		 if(_gameState._inventory._backItem.isWithinItem(x, y)) {
				_mainScreen.loadScene(_gameState._currentLocation, _bottomMenu);
				_inventoryOn = false;
		 }
	}
	
	private void itemMenuCheckMouseClickedAndUpdate(int x, int y) {
		if(_selectedItem != null)
		{
			for(int i=0; i<_selectedItem._itemMenu._itemMenuButtons.length; i++){
				if(_selectedItem._itemMenu._itemMenuButtons[i].checkIfOnButton(new Point(x, y))){
					String selectedOption;
					selectedOption = (_selectedItem._itemMenu._menuOptions[i]);
					processItemMenuSelection(selectedOption);
				}
			}
			_selectedItem.undrawMenu();
			_selectedItem._itemButton.unHighlight();
			_selectedItem = null;
			_itemMenuOpen = false;
		}
		else if(_selectedCharacter != null) {
			for(int i=0; i< _selectedCharacter._itemMenu._itemMenuButtons.length; i++){
				if(_selectedCharacter._itemMenu._itemMenuButtons[i].checkIfOnButton(new Point(x, y))){
					String selectedOption;
					selectedOption = (_selectedCharacter._itemMenu._menuOptions[i]);
					processItemMenuSelection(selectedOption);
				}
			}
			_selectedCharacter._menuOn = false;
			_selectedCharacter = null;
			_itemMenuOpen = false;
		}
		
		itemsCheckMouseMovedAndUpdate(x,y);
		_mainScreen.redraw();
	}

	private void itemsCheckMouseClickedAndUpdate(int x, int y) {
		// TODO Auto-generated method stub
		if(_mainScreen._currentScene._itemsBack != null){
			for(int i = 0; i < _mainScreen._currentScene._itemsBack.length; i++) {
				Item tempItem = _mainScreen._currentScene._itemsBack[i];
				if(tempItem._itemButton.checkIfOnButton(new Point(x,y)) && tempItem._menuOptions != null){
					tempItem.drawMenu();
					_selectedItem = tempItem;
					_itemMenuOpen = true;
					tempItem._itemButton.unHighlight();
					itemMenuCheckMouseMovedAndUpdate(x,y);
					break;
				}
			}
		}
		if(_mainScreen._currentScene._itemsFront != null){
			for(int i = 0; i < _mainScreen._currentScene._itemsFront.length; i++) {
				Item tempItem = _mainScreen._currentScene._itemsFront[i];
				if(tempItem.isWithinItem(x, y) && tempItem._menuOptions != null){
					tempItem.drawMenu();
					_selectedItem = tempItem;
					_itemMenuOpen = true;
					tempItem._itemButton.unHighlight();
					break;
				}
			}
		}
		_mainScreen.redraw();		
	}	
	private void charactersCheckMouseClickedAndUpdate(int x, int y)
	{
		if(_mainScreen._currentScene._characters != null){
			for(int i = 0; i < _mainScreen._currentScene._characters.length; i++) {
				Character tempCharacter = _mainScreen._currentScene._characters[i];
				if(tempCharacter.isWithinCharacter(x, y) && !tempCharacter._isMainCharacter) {
					tempCharacter._menuOn = true;
					_selectedCharacter = tempCharacter;
					_itemMenuOpen = true;
					//tempItem._itemButton.unHighlight();
					itemMenuCheckMouseMovedAndUpdate(x,y);
					break;
				}
			}
		}
		_mainScreen.redraw();
	}
	
	public void itemMenuCheckMouseMovedAndUpdate(int x, int y) {
		if(_selectedCharacter != null) {
			for(int i=0; i< _selectedCharacter._itemMenu._itemMenuButtons.length; i++) {
				Button tempButton = _selectedCharacter._itemMenu._itemMenuButtons[i];
				if(tempButton.checkIfOnButton(new Point(x, y))){
					tempButton.highlight();
				}else{
					tempButton.unHighlight();
				}
			}
		}
		
		if(_selectedItem != null) {
			if(_mainScreen._currentScene._itemsBack != null) {
				for(int i=0; i<_selectedItem._itemMenu._itemMenuButtons.length; i++) {
					Button tempButton = _selectedItem._itemMenu._itemMenuButtons[i];
					if(tempButton.checkIfOnButton(new Point(x, y))){
						tempButton.highlight();
					}else{
						tempButton.unHighlight();
					}
				}
			}
		}
		_mainScreen.redraw();
	}
	
	public void itemsCheckMouseMovedAndUpdate(int x, int y){
		if(_mainScreen._currentScene._itemsBack != null) {
			for(int i = 0; i < _mainScreen._currentScene._itemsBack.length; i++){
				Item tempItem =_mainScreen._currentScene._itemsBack[i]; 
				if(tempItem._itemButton.checkIfOnButton(new Point(x, y))){
					tempItem._itemButton.highlight();
					
					if(tempItem._audioClip != null) 
						tempItem._audioClip.play();
				}else{
					tempItem._itemButton.unHighlight();	
					
					if(tempItem._audioClip != null) 
						tempItem._audioClip.stop();
				}
			}
		}
		if(_mainScreen._currentScene._itemsFront != null) {
			for(int i = 0; i < _mainScreen._currentScene._itemsFront.length; i++){
				Item tempItem =_mainScreen._currentScene._itemsFront[i]; 
				if(tempItem._itemButton.checkIfOnButton(new Point(x, y))){
					tempItem._itemButton.highlight();
				}else{
					tempItem._itemButton.unHighlight();		
				}
			}
		}
		_mainScreen.redraw();
	}
	
	public void	puzzlePiecesCheckClickandUpdate(int x, int y){
		if(!_puzzleMiniGame._pieceSelected){
			for(int i = 0; i < _mainScreen._currentScene._itemsFront.length; i++){
				if(_mainScreen._currentScene._itemsFront[i]._itemButton.checkIfOnButton(new Point(x, y))){
					_puzzleMiniGame.selectPiece(i);
				}
			}
		}	
		
		else{
			for(int i = 0; i < _mainScreen._currentScene._itemsBack.length; i++){
				if(_mainScreen._currentScene._itemsBack[i]._itemButton.checkIfOnButton(new Point(x, y)))
					_puzzleMiniGame.placeOnGrid(i);
			}			
		}
		
		if(_puzzleMiniGame._win){
			_puzzleGameOn = false;
			//createIsland();
		}
			
		
		_mainScreen.redraw();
	}
	
	public void processItemMenuSelection(String selectedOption) {
		
		System.out.println(selectedOption);
		
		if(selectedOption == "talkTo")
			_selectedCharacter.talk();	
		else if( selectedOption == "addToInventory" ) {
			if( !_selectedItem._isInInventory )				
				_gameState._inventory.addItemToInventory(_selectedItem);

		}
		else if( selectedOption == "visitAndProgress" || selectedOption == "addToBowlAndProgress" )
			incrementPlotPoint();
		else if(selectedOption == "visit") {
			if(_selectedItem._name == "kitchenExit") 
				changeLocation(_gameState._plotPointReference._livingRoom);
			else if(_selectedItem._name == "livingRoomExitToKitchen")
				changeLocation(_gameState._plotPointReference._kitchen);
			else if(_selectedItem._name == "conveyorBeltMiniGameItem")
				createConveyorBeltMiniGame();

		}
		else if (selectedOption == "visitHouse")
			changeLocation(_gameState._plotPointReference._livingRoom);
		else if (selectedOption == "visitMarket") 
			changeLocation(_gameState._plotPointReference._market);
		else if (selectedOption == "visitFarm")
			changeLocation(_gameState._plotPointReference._farm);
		else if (selectedOption == "visitIsland")
			changeLocation(_gameState._plotPointReference._island);
		
		else if(selectedOption == "catchChickens")
			createChickenMiniGame();
		else if(selectedOption == "fishMiniGame")
			createFishMiniGame();
		
		if(selectedOption == "addToInventory")
			if(!_selectedItem._isInInventory){
				_gameState._inventory.addItemToInventory(_selectedItem);
			}
		
		else if(selectedOption == "addToBowl"){
			_gameState._mixingBowl.addIngredient(_selectedItem);
		}
		
		else if(selectedOption == "mix"){
			_gameState._mixingBowl.mix(_gameState._plotPointReference._plotPoints[_gameState._currentPlotPoint]._currentRecipe);
			if(_gameState._mixingBowl._recipeComplete)
				incrementPlotPoint();
		}
				
		else if(selectedOption == "addToBakingPan"){
			_gameState._bakingPan._userRecipe = _gameState._mixingBowl._userRecipe;
			_gameState._mixingBowl.addToBakingPan();
		}
				
		else if(selectedOption == "addToOven"){
			_gameState._oven._userRecipe = _gameState._bakingPan._userRecipe;
			_gameState._bakingPan.placeInOven();
		}		
		else if(selectedOption == "bake"){
			_gameState._oven.bake(_gameState._plotPointReference._plotPoints[_gameState._currentPlotPoint]._currentRecipe);
			if(_gameState._oven._recipeComplete)
				incrementPlotPoint();
		}		
		else if(selectedOption == "addToFryingPan"){
			_gameState._fryingPan.addIngredient(_selectedItem);
		}
		else if(selectedOption == "fry"){
			_gameState._fryingPan.fry(_gameState._plotPointReference._plotPoints[_gameState._currentPlotPoint]._currentRecipe);
			if(_gameState._fryingPan._recipeComplete)
				incrementPlotPoint();
		}

		else if(selectedOption == "getBreadOption"){
			String[] breadImages = {"breadItem"};
			String[] addToFryingPanOption = {"addToFryingPan","nullOption","nullOption","nullOption"};
			Item breadItem = new Item("bread", breadImages, 0, 0, addToFryingPanOption, null, null, false, null);		
			_gameState._inventory.addItemToInventory(breadItem);
		}
		else if(selectedOption == "getCheeseOption"){	
			String[] cheeseImages = {"cheeseItem"};
			String[] addToFryingPanOption = {"addToFryingPan","nullOption","nullOption","nullOption"};
			Item cheeseItem = new Item("cheese", cheeseImages, 0, 0, addToFryingPanOption, null, null, false, null);
			_gameState._inventory.addItemToInventory(cheeseItem);
		}
		else if(selectedOption == "getMilkOption"){
			String[] milkImages = {"foodMilk"};
			String[] addToFryingPanOption = {"addToMixingBowl","nullOption","nullOption","nullOption"};
			Item milkItem = new Item("milk", milkImages, 0, 0, addToFryingPanOption, null, null, false, null);
			_gameState._inventory.addItemToInventory(milkItem);
		}
		else if(selectedOption == "getCheeseOption"){
			String[] cheeseImages = {"cheeseItem"};
			String[] addToFryingPanOption = {"addToFryingPan","addToBakingPan","nullOption","nullOption"};
			Item cheeseItem = new Item("cheese", cheeseImages, 0, 0, addToFryingPanOption, null, null, false, null);
			_gameState._inventory.addItemToInventory(cheeseItem);
		}
	}
	
	public void saveGame() {
		_gameState.saveState();
	}
	
	public void loadGame() {
		_gameState.loadState();
	}
	
	public void exitGame() {
		System.exit(0);
	}
	
}
